package jdbc2;

import myschool.util.DbUtil;

import java.sql.*;
import java.util.Arrays;

/**
 * @author oldliu
 * @since 1.0
 */
public class QueryQues {
    public static void main(String[] args)throws Exception {
        String sql="select *   from quiz.question_2 order by 1 asc limit 300";
        Connection con= DbUtil.get();
        Connection con2= DriverManager.getConnection("jdbc:mysql://localhost:3308/dongfang?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true","root","root1234");
        PreparedStatement ps=null,ps2=null;
        ResultSet rs=null;
        String sql2="insert into d_surveyques(title,stype,option1,option2,option3,option4,opvalue1,opvalue2,opvalue3,opvalue4)";
        sql2+="values(?,?,?,?,?,?,?,?,?,?)";
        try {
            ps=con.prepareStatement(sql);
            rs=ps.executeQuery();
            ps2=con2.prepareStatement(sql2);
            while (rs.next()){
                String c=rs.getString("content");
                String t=rs.getString("title");
                String ans=rs.getString("answers");
                String[] ar = c.split("@!@");
                if(ar.length!=4)
                    continue;
                int f=1;
                ps2.setString(f++,t);
                ps2.setInt(f++, (int) (Math.random()*4+1));
                for (int i = 0; i <ar.length ; i++) {
                    ps2.setString(f++,ar[i]);
                }
                ps2.setInt(f++,ans.equals("0")?1:0);
                ps2.setInt(f++,ans.equals("1")?1:0);
                ps2.setInt(f++,ans.equals("2")?1:0);
                ps2.setInt(f++,ans.equals("3")?1:0);
                ps2.addBatch();
            }
            int[] ints = ps2.executeBatch();
            System.out.println(Arrays.toString(ints));
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DbUtil.close(null,ps2,con2);
            DbUtil.close(rs,ps,con);
        }
    }
}
